package com.gitee.sunstar.echarts

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.constraintlayout.widget.ConstraintLayout
import com.chooongg.echarts.EchartsWebHelper
import com.ejlchina.okhttps.OkHttps
import com.google.gson.Gson

class MainActivity : AppCompatActivity() {
    val helper by lazy {
        EchartsWebHelper()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<WebView>(R.id.parent).run {
            settings.allowFileAccess = true
            settings.javaScriptEnabled = true
            loadUrl("file:///android_asset/echarts.html")
            webViewClient = object : WebViewClient() {
                override fun onPageFinished(view: WebView?, url: String?) {
                    super.onPageFinished(view, url)
                    OkHttps.async(
                        "https://cdn.jsdelivr.net/gh/apache/echarts-website@asf-site/examples/data/asset/data/webkit-dep.json"
                    ).setOnResponse {
                        val text = it.body.toBean<Test>(Test::class.java)
                        runOnUiThread {
                            loadUrl(
                                "javascript:setOption(${"{\n" +
                                        "    legend: {\n" +
                                        "      data: ['HTMLElement', 'WebGL', 'SVG', 'CSS', 'Other']\n" +
                                        "    },\n" +
                                        "    series: [\n" +
                                        "      {\n" +
                                        "        type: 'graph',\n" +
                                        "        layout: 'force',\n" +
                                        "        animation: false,\n" +
                                        "        label: {\n" +
                                        "          position: 'right',\n" +
                                        "          formatter: '{b}'\n" +
                                        "        },\n" +
                                        "        draggable: true,\n" +
                                        "        data: ${Gson().toJson(text.nodes)}.map(function (node, idx) {\n" +
                                        "          node.id = idx;\n" +
                                        "          return node;\n" +
                                        "        }),\n" +
                                        "        categories: ${Gson().toJson(text.categories)},\n" +
                                        "        force: {\n" +
                                        "          edgeLength: 5,\n" +
                                        "          repulsion: 20,\n" +
                                        "          gravity: 0.2\n" +
                                        "        },\n" +
                                        "        edges: ${Gson().toJson(text.links)}\n" +
                                        "      }\n" +
                                        "    ]\n" +
                                        "  }"})"
                            )
                        }

                    }.get()

                }
            }
        }

//        val agent = helper.buildEChartAgent(this,findViewById(R.id.parent) as ConstraintLayout)
//
//        helper.loadDemoView(agent)
    }
}